package cn.zjyj.module.zjyj.service.riskassessmentdetail;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.zjyj.module.zjyj.controller.admin.riskassessmentdetail.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskassessmentdetail.RiskAssessmentDetailDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.riskassessmentdetail.RiskAssessmentDetailMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 风险评估备份 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RiskAssessmentDetailServiceImpl implements RiskAssessmentDetailService {

    @Resource
    private RiskAssessmentDetailMapper riskAssessmentDetailMapper;

    @Override
    public Long createRiskAssessmentDetail(RiskAssessmentDetailSaveReqVO createReqVO) {
        // 插入
        RiskAssessmentDetailDO riskAssessmentDetail = BeanUtils.toBean(createReqVO, RiskAssessmentDetailDO.class);
        riskAssessmentDetailMapper.insert(riskAssessmentDetail);
        // 返回
        return riskAssessmentDetail.getId();
    }

    @Override
    public void updateRiskAssessmentDetail(RiskAssessmentDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskAssessmentDetailExists(updateReqVO.getId());
        // 更新
        RiskAssessmentDetailDO updateObj = BeanUtils.toBean(updateReqVO, RiskAssessmentDetailDO.class);
        riskAssessmentDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskAssessmentDetail(Long id) {
        // 校验存在
        validateRiskAssessmentDetailExists(id);
        // 删除
        riskAssessmentDetailMapper.deleteById(id);
    }

    @Override
        public void deleteRiskAssessmentDetailListByIds(List<Long> ids) {
        // 校验存在
        validateRiskAssessmentDetailExists(ids);
        // 删除
        riskAssessmentDetailMapper.deleteByIds(ids);
        }

    private void validateRiskAssessmentDetailExists(List<Long> ids) {
        List<RiskAssessmentDetailDO> list = riskAssessmentDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RISK_ASSESSMENT_DETAIL_NOT_EXISTS);
        }
    }

    private void validateRiskAssessmentDetailExists(Long id) {
        if (riskAssessmentDetailMapper.selectById(id) == null) {
            throw exception(RISK_ASSESSMENT_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public RiskAssessmentDetailDO getRiskAssessmentDetail(Long id) {
        return riskAssessmentDetailMapper.selectById(id);
    }

    @Override
    public PageResult<RiskAssessmentDetailDO> getRiskAssessmentDetailPage(RiskAssessmentDetailPageReqVO pageReqVO) {
        return riskAssessmentDetailMapper.selectPage(pageReqVO);
    }

}